package org.datascooter.test.jpaHard;

import java.io.Serializable;
import java.util.Collection;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SecondaryTable;
import javax.persistence.Table;
import javax.persistence.Version;

import org.datascooter.test.jpaSimple.Address;

@Entity
@Table(name = "EMPL")
@SecondaryTable(name = "EMP_SALARY", pkJoinColumns = @PrimaryKeyJoinColumn(name = "EMP_ID", referencedColumnName = "ID"))
public class Employee implements Serializable {

	private Long id;
	private int version;
	private String name;
	private Address address;
	private Collection phoneNumbers;
	private Collection<Project> projects;
	private Long salary;
	private EmploymentPeriod period = new EmploymentPeriod();

	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Version
	@Column(name = "EMP_VERSION", nullable = false)
	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	@Column(name = "EMP_NAME", length = 80)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@ManyToOne(cascade = CascadeType.PERSIST, optional = false)
	@JoinColumn(name = "ADDR_ID", referencedColumnName = "ID", nullable = false)
	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	@OneToMany(targetEntity = PhoneNumber.class, cascade = CascadeType.ALL, mappedBy = "employee")
	public Collection getPhoneNumbers() {
		return phoneNumbers;
	}

	public void setPhoneNumbers(Collection phoneNumbers) {
		this.phoneNumbers = phoneNumbers;
	}

	@ManyToMany(cascade = CascadeType.PERSIST, mappedBy = "employees")
	@JoinTable(name = "EMP_PROJ", joinColumns = @JoinColumn(name = "EMP_ID", referencedColumnName = "ID"), inverseJoinColumns = @JoinColumn(name = "PROJ_ID", referencedColumnName = "ID"))
	public Collection<Project> getProjects() {
		return projects;
	}

	public void setProjects(Collection<Project> projects) {
		this.projects = projects;
	}

	@Column(name = "EMP_SAL", table = "EMP_SALARY")
	public Long getSalary() {
		return salary;
	}

	public void setSalary(Long salary) {
		this.salary = salary;
	}

	@Embedded
	@AttributeOverrides({
			@AttributeOverride(name = "startDate", column = @Column(name = "EMP_START")), @AttributeOverride(name = "endDate", column = @Column(name = "EMP_END"))
	})
	public EmploymentPeriod getEmploymentPeriod() {
		return period;
	}

	public void setEmploymentPeriod(EmploymentPeriod period) {
		this.period = period;
	}
}
